package manager;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import common.DBManager;

public class DeleteCheckServlet extends HttpServlet 
{
	static String id;
	static Connection connection=null;
	static Statement statement=null;
	static ResultSet resultSet=null;
	static ManagerBean managerBean=null;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		//super.doGet(req, resp);
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("UTF-8");
		
		sendInfo(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException 
	{
		//super.doPost(req, resp);
		req.setCharacterEncoding("UTF-8");
		resp.setContentType("UTF-8");
		
		sendInfo(req,resp);
	}
	
	public void sendInfo(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
	{
		id=req.getParameter("id");
		req.setAttribute("id", id);
		
		RequestDispatcher requestDispatcher=req.getRequestDispatcher("/manager/managerDeleteCheck.jsp");
		requestDispatcher.forward(req, resp);
	}
	
	public static ManagerBean getManagerInfomation(String indexId)
	{
		connection=DBManager.getConnection();
		managerBean=new ManagerBean();
		
		try 
		{
			statement=connection.createStatement();
			resultSet=statement.executeQuery("SELECT * FROM managers WHERE ID = "+id);
			
			while(resultSet.next())
			{
				managerBean.setId(resultSet.getInt("ID"));
				managerBean.setName(resultSet.getString("NAME"));
				managerBean.setRuby(resultSet.getString("RUBY"));
				managerBean.setUpperLevel(resultSet.getShort("UPPER_LEVEL"));
				managerBean.setPassword(resultSet.getString("PASSWORD"));
				managerBean.setTime(resultSet.getString("TIME"));
				managerBean.setDeleteFlag(resultSet.getInt("DELETE_FLAG"));
			}
		} 
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		catch(NumberFormatException e)
		{
			e.printStackTrace();
		}
		finally
		{
			if(statement!=null)
			{
				try
				{
					statement.close();
				} 
				catch (SQLException e) 
				{
					e.printStackTrace();
				}
			}
			
			if(connection!=null)
			{
				try
				{
					connection.close();
				} 
				catch (SQLException e) 
				{
					e.printStackTrace();
				}
			}
		}
		
		return managerBean;
	}

}
